package com.zhonghan.recharge.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhonghan.common.bean.Result;
import com.zhonghan.common.enums.ResultStatus;
import com.zhonghan.common.utils.StringUtils;
import com.zhonghan.recharge.pojo.OilCustomerOrder;
import com.zhonghan.recharge.pojo.OilCustomerRelease;
import com.zhonghan.recharge.service.OilCustomerOrderService;
import com.zhonghan.recharge.service.OilCustomerReleaseService;
import com.zhonghan.recharge.service.ZhFillCardService;

/**
 * 买油送油 买油送车
 * @author Administrator
 *
 */
@RequestMapping("/buyOil")
@Controller
public class BuyOilController {
	
	@Autowired
	private OilCustomerOrderService oilCustomerOrderService;
	
	@Autowired
	private OilCustomerReleaseService oilCustomerReleaseService;
	
	@Autowired
	private ZhFillCardService zhFillCardService;
	
	/**
	 * 跳转到客户订单管理
	 * @return
	 */
	@RequestMapping(value="/clientAll",method = RequestMethod.GET)
	public String toShowClient(){
		
		return "admin/client";
	}
	/**
	 * 查询所有的客户订单信息
	 * @return
	 */
	@RequestMapping(value="/clientAll",method = RequestMethod.POST)
	@ResponseBody
	public Result showClient(String customerName,Integer pageNum,Integer pageSize){
		Result result = oilCustomerOrderService.selectOilCustomerOrderByName(customerName, pageNum, pageSize);
		return result;
	}
	

	/**
	 * 跳转至释放油品页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/calculateOil")
	public String showCalculateOil(HttpServletRequest request,HttpServletResponse response,Model model,Integer id){
		
		OilCustomerOrder oilCustomerOrder = oilCustomerOrderService.selectOilCustomerOrderById(id);
		model.addAttribute("OilCustomerOrder", oilCustomerOrder);
		model.addAttribute("ID", id);
		
		return "admin/calculateOil";
	}
	
	/**
	 * 开始释放油品
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/releaseOil")
	@ResponseBody
	public Result releaseOil(HttpServletRequest request, HttpServletResponse response,Integer id,Long isRelease){
		
		Result result = new Result();
		
		OilCustomerOrder oilCustomerOrder = oilCustomerOrderService.selectOilCustomerOrderById(id);
		oilCustomerOrder.setIsRelease(isRelease);
		try {
			int code = oilCustomerOrderService.updateNonEmptyOilCustomerOrderById(oilCustomerOrder);
			if(code>0){
				result.setSuccess("1", null,"操作成功!开始释放");
			}else{
				result.setFailure("0", "操作失败!");
			}
			
		} catch (Exception e) {
			result.setFailure(ResultStatus.BUSYNESS.getCode(), ResultStatus.BUSYNESS.getDescription());
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * 跳转到添加客户
	 * @return
	 */
	@RequestMapping(value="/toAddClient",method = RequestMethod.GET)
	public String toAddClient(){
		
		return "admin/oilCar";
	}
	/**
	 * 添加客户订单
	 * @param customerName 客户名字
	 * @param customerPhone 客户电话
	 * @param customerDescription 用油描述
	 * @param customerType 买油类型
	 * @param inputOilAmount 充值金额
	 * @param donationAmount 赠送金额
	 * @param backByMonth 分期数
	 * @return
	 */
	@RequestMapping("/addClient")
	@ResponseBody
	public Result addClient(String customerName,String customerPhone,String customerDescription,
			String customerType,Double inputOilAmount,Double donationAmount,Integer backByMonth,String remarks){
		
		Result result = new Result();
		
		OilCustomerOrder oliCustomerOrder = new OilCustomerOrder();
		oliCustomerOrder.setCustomerName(customerName);
		oliCustomerOrder.setCustomerPhone(customerPhone);
		oliCustomerOrder.setCustomerDescription(customerDescription);
		oliCustomerOrder.setCustomerType(customerType);
		oliCustomerOrder.setInputOilAmount(inputOilAmount);
		oliCustomerOrder.setDonationAmount(donationAmount);
		oliCustomerOrder.setTotalAmount(inputOilAmount + donationAmount );
		oliCustomerOrder.setBackByMonth(backByMonth);
		oliCustomerOrder.setIsRelease(Long.parseLong(Integer.toString(0)));
		oliCustomerOrder.setCreateTime(new Date());
		oliCustomerOrder.setRemarks(remarks);
		int insertOilCustomerOrder = oilCustomerOrderService.insertOilCustomerOrder(oliCustomerOrder);
		
		if(insertOilCustomerOrder >0){
			result.setCode("1");
			result.setMsg("添加成功");
		}else{
			result.setCode("0");
			result.setMsg("添加失败");
		}
		return result;
	}
	/**
	 * 准备去修改 通过id查询
	 * @param id
	 * @return
	 */
	@RequestMapping("/toUpdateClient")
	public String toUpdateClient(Integer id,Model model){
		OilCustomerOrder selectOilCustomerOrderById = oilCustomerOrderService.selectOilCustomerOrderById(id);
		model.addAttribute("order", selectOilCustomerOrderById);
		return "admin/updateClient";
	}
	/**
	 * 
	 * @param id
	 * @param customerName 客户名字
	 * @param customerPhone 客户电话
	 * @param customerDescription 用油描述
	 * @param customerType 买油类型
	 * @param inputOilAmount 充值金额
	 * @param donationAmount 赠送金额
	 * @param backByMonth 分期数
	 * @return
	 */
	@RequestMapping("/updateClient")
	@ResponseBody
	public Result updateClient(long id,String customerName,String customerPhone,String customerDescription,
			String customerType,Double inputOilAmount,Double donationAmount,Integer backByMonth,
			String remarks,Model model){
		Result result = new Result();
		OilCustomerOrder oliCustomerOrder = new  OilCustomerOrder();
		oliCustomerOrder.setCustomerName(customerName);
		oliCustomerOrder.setCustomerPhone(customerPhone);
		oliCustomerOrder.setCustomerDescription(customerDescription);
		oliCustomerOrder.setCustomerType(customerType);
		oliCustomerOrder.setInputOilAmount(inputOilAmount);
		oliCustomerOrder.setDonationAmount(donationAmount);
		oliCustomerOrder.setTotalAmount(inputOilAmount + donationAmount );
		oliCustomerOrder.setBackByMonth(backByMonth);
		oliCustomerOrder.setIsRelease(Long.parseLong(Integer.toString(0)));
		oliCustomerOrder.setCreateTime(new Date());
		oliCustomerOrder.setRemarks(remarks);
		oliCustomerOrder.setId(Integer.parseInt(Long.toString(id)));
		int updateOilCustomerOrderById = oilCustomerOrderService.updateOilCustomerOrderById(oliCustomerOrder);
		
		if(updateOilCustomerOrderById >0){
			result.setCode("1");
			result.setMsg("修改成功");
		}else{
			result.setCode("0");
			result.setMsg("修改失败");
		}
		return result;
	}
	/**
	 * 查询客户买油分期记录
	 * @param request
	 * @param response
	 * @param id
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/getOilCustomerRelease")
	@ResponseBody
	public Result getOilCustomerRelease(HttpServletRequest request,HttpServletResponse response,Integer id,Integer pageNum,Integer pageSize){
		
		Result result = oilCustomerReleaseService.selectOilCustomerReleaseByCondition(id,pageNum,pageSize);
		
		return result;
		
	}
	
	/**
	 * 
	 * @param id				客户订单编号
	 * @param cardps0			1000面额充值卡开始号段
	 * @param cardpe0			1000面额充值卡结束号段
	 * @param cardps1			500面额充值卡开始号段
	 * @param cardpe1			500面额充值卡结束号段
	 * @param cardps2			100面额充值卡开始号段
	 * @param cardpe2			100面额充值卡结束号段
	 * @param releaseTime		释放时间
	 * @param releaseAmount		释放金额
	 * @return
	 */
	@RequestMapping("/addOilCustomerRelease")
	@ResponseBody
	public Result addOilCustomerRelease(Integer id,String cardps0,String cardpe0,String cardps1,String cardpe1,String cardps2,String cardpe2,String releaseTime,Double releaseAmount){
		String cardPart1000 = null;
		String cardPart500 = null;
		String cardPart100 = null;
		
		if(StringUtils.isNotEmpty(cardps0) && StringUtils.isNotEmpty(cardpe0)){
			cardPart1000 = cardps0.concat("-").concat(cardpe0);
		}
		if(StringUtils.isNotEmpty(cardps1) && StringUtils.isNotEmpty(cardpe1)){
			cardPart500 = cardps1.concat("-").concat(cardpe1);
		}
		if(StringUtils.isNotEmpty(cardps2) && StringUtils.isNotEmpty(cardpe2)){
			cardPart100 = cardps2.concat("-").concat(cardpe2);
		}
		
		Result result = null;
		try {
			result = oilCustomerReleaseService.addOilCustomerRelease(id,cardPart1000,cardPart500,cardPart100,releaseTime,releaseAmount);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
		
	}

	/**
	 * 跳转至客户充值卡管理页面
	 * @return
	 */
	@RequestMapping(value="/customerCardList",method=RequestMethod.GET)
	public String showCustomerCardList(){
		
		return "admin/customerCardList";
		
	}
	
	/**
	 * 充值卡管理数据查询
	 * @param beginTime
	 * @param endTime
	 * @param status
	 * @param customerName
	 * @return
	 */
	@RequestMapping(value="/customerCardList",method=RequestMethod.POST)
	@ResponseBody
	public Result getCustomerCardList(String beginTime,String endTime,Integer status,String customerName,Integer pageNum,Integer pageSize){
		
		Result result = zhFillCardService.getCustomerCardList(beginTime,endTime,status,customerName,pageNum,pageSize);
		
		return result;
	}
	
	
}
